package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.otaliastudios.cameraview.BitmapCallback;
import com.otaliastudios.cameraview.PictureResult;

import java.io.File;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityPreviewBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.DensityUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.SPUtil;

public class PreviewActivity extends BaseAc<ActivityPreviewBinding> {

    public static PictureResult sPicResult;
    public static float watermarkLeftRate;
    public static float watermarkTopRate;
    public static Bitmap watermarkBitmap;
    private Bitmap mRetBitmap;
    public static String shotResultPath;

    @Override
    protected int onCreate() {
        return R.layout.activity_preview;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPreviewBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivPreviewSave.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPreviewSave://保存
                save();
                break;
        }
    }

    @Override
    protected void initData() {
        setPictureMark();
    }

    private void save() {
        showDialog(getString(R.string.saving));
        RxUtil.create(new RxUtil.Callback<File>() {
            @Override
            public void doBackground(ObservableEmitter<File> emitter) {
                File file = ImageUtils.save2Album(mRetBitmap, Bitmap.CompressFormat.PNG);
                emitter.onNext(file);
            }

            @Override
            public void accept(File obj) {
                dismissDialog();
                ToastUtils.showShort(R.string.save_sys_gallery_tip);
                startActivity(HomeActivity.class);
            }
        });
    }

    private void setPictureMark() {
        if (sPicResult == null) return;
        int width = sPicResult.getSize().getWidth();
        int height = sPicResult.getSize().getHeight();
        int screenWidth = DensityUtil.getWith(this);
        int screenHeight = DensityUtil.getHeight(this);
        if (width * height > screenWidth * screenHeight) {
            width = screenWidth;
            height = screenHeight;
        }

        sPicResult.toBitmap(width, height, new BitmapCallback() {
            @Override
            public void onBitmapReady(@Nullable Bitmap bitmap) {
                RxUtil.create(new RxUtil.Callback<Bitmap>() {
                    @Override
                    public void doBackground(ObservableEmitter<Bitmap> emitter) {
                        if (watermarkBitmap != null) {
                            //有水印时的处理
                            Bitmap retBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
                            Canvas canvas = new Canvas(retBitmap);
                            //核心点2：计算水印控件在所拍照片上的正确的位置
                            float left = retBitmap.getWidth() * watermarkLeftRate;
                            float top = retBitmap.getHeight() * watermarkTopRate;
                            canvas.drawBitmap(watermarkBitmap, left, top, null);
                            emitter.onNext(retBitmap);
                        } else {
                            //无贴纸无水印时的处理
                            emitter.onNext(bitmap);
                        }
                    }

                    @Override
                    public void accept(Bitmap obj) {
                        mDataBinding.imageViewTouch.setImageBitmap(obj);
                        mRetBitmap = obj;
                    }
                });
            }
        });
    }

}